package com.fd.spring.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.xml.ws.soap.Addressing;
import java.util.Objects;
@Data
@Component("person") //加入到Spring容器中的Bean对象的定义上,添加最常用的Bean注解@Component,完成注解的配置
public class Person {
    public String naem;
    public Integer age;
//项目中也不推荐使用@Resource注入,因为耦合度比较高
    @Resource(name = "lamborghini") //@Resource注解 也是用来做Bean的注入,只不过是通过名称来注入,这个注解不是Spring所有,而是JDK自带
    private Car defaultCar;
    @Autowired
    @Qualifier("cadillac")  //这里指定注入的Bean对象是Car的子类bMW,而不会是tSL,系统也不会报错
    private Car car;

    //  自定义有参构造器
    public Person(String naem, Integer age, Car car) {
        this.naem = naem;
        this.age = age;
        this.car = car;
    }

    //  自定义有参构造器
    public Person(Integer age, Car car,String naem) {
        this.naem = naem;
        this.age = age;
        this.car = car;
    }

    //  自定义无参构造器
    public Person() {
    }

    @PostConstruct  //在当前对象初始化之后执行代码
    public void init(){
        System.out.println("Person的初始化方法");
    }
    @PreDestroy  //在当前对象销毁之前
    public void destroy(){
        System.out.println("Person的销毁方法");
    }
}
